import QtQuick 1.1
import com.nokia.meego 1.0
import ATND 1.0
import '../Components'
import '../APIs'
import '../Views'
import '../Delegates'

Page {
    id: root

    property bool loading: model.status !== XmlListModel.Ready
    property string saveKey
    property string keyword: settings.readData(saveKey, '')
    property alias searchQuery: model.searchQuery
    signal selected(variant event)

    onKeywordChanged: {
        console.debug('keywordChanged', saveKey, keyword)
        var key = settings.readData(saveKey, '')
        if (key !== keyword) {
            settings.saveData(saveKey, keyword)
            settings.saveData(saveKey.concat('/event_id'), 0)
        }
    }

    GConfItem {
        id: feedEnabled
        key: '/apps/ControlPanel/ATND/EnableFeed'
    }

    EventView {
        id: view
        anchors.fill: parent
        model: EventModel {
            id: model
            onStatusChanged: {
                if (status === XmlListModel.Ready) {
                    if (view.count === 0) {
                        settings.saveData(saveKey.concat('/event_id'), '')
                    } else if (keyword.length > 0){
//                        console.debug('feedEnabled.value', feedEnabled.value)
                        if (feedEnabled.value === true) {
                            var event_id = settings.readData(saveKey.concat('/event_id'), 0)
//                            event_id = 0
    //                        console.debug('previous event_id', event_id, saveKey)
                            for (var i = 0; i < model.count; i++) {
                                var event = get(i)
                                if (event.event_id > event_id) {
                                    eventFeed.icon = '/usr/share/icons/hicolor/80x80/apps/qatnd80.png'
                                    eventFeed.title = event.title
                                    eventFeed.body = event.catch_
                                    dateTime.source = event.started_at
                                    eventFeed.timestamp = new Date(dateTime.year, dateTime.month - 1, dateTime.day, dateTime.hour, dateTime.minute, dateTime.second)
                                    eventFeed.footer = event.place
                                    eventFeed.video = false
                                    eventFeed.url = 'atnd://'.concat(event.event_id)
                                    eventFeed.sourceName = 'atnd'
                                    eventFeed.sourceDisplayName = 'ATND'
                                    eventFeed.add()
                                } else {
                                    break
                                }
                            }
                        }
//                        console.debug('new event_id', get(0).event_id, saveKey)
                        settings.saveData(saveKey.concat('/event_id'), get(0).event_id)
                    }
                }
            }
        }

        delegate: EventDelegate {
            width: ListView.view.width
            title: model.title
            catch_: model.catch_
            started_at: DateTime { source: model.started_at }
            ended_at: DateTime { source: model.ended_at }
            place: model.place
            onClicked: root.selected(model)
        }
    }
    ScrollDecorator {
        flickableItem: view
    }
}
